package cn.edu.zufe.mjt.spinrg.web.controller;

import cn.edu.zufe.mjt.spinrg.Component;
import cn.edu.zufe.mjt.spinrg.sub.User;
import cn.edu.zufe.mjt.spinrg.web.ModelAndView;
import cn.edu.zufe.mjt.spinrg.web.annotation.Controller;
import cn.edu.zufe.mjt.spinrg.web.annotation.Param;
import cn.edu.zufe.mjt.spinrg.web.annotation.RequestMapping;
import cn.edu.zufe.mjt.spinrg.web.annotation.ResponseBody;

@Controller
@Component
@RequestMapping("/hello")
public class HelloController {

    // localhost:port/hello/mjt
    @RequestMapping("/mjt")
    public String hello(@Param("name") String name, @Param("age") Integer age){
        return String.format("<h2>Hello %s, your age is %d<h2>", name, age);
    }
    @RequestMapping("/json")
    @ResponseBody
    public User json(@Param("name") String name, @Param("age") Integer age){
        User user = new User();
        user.setName(name);
        user.setAge(age);
        return user;
    }


    @RequestMapping("/html")
    public ModelAndView modelAndView(@Param("name") String name, @Param("age") Integer age){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView("index.html");
        modelAndView.getContext().put("name",name);
        modelAndView.getContext().put("age",age.toString());
        return modelAndView;
    }
}
